var userDAO = require('../models/userDAO')
var formidable = require('formidable')
var path = require('path')
var jwt = require('jsonwebtoken')
var bcrypt = require('bcrypt')
var userController = {
    getAllUsers: function (req, res) {
        var userId = req.user.userId
        userDAO.getAllUsers(userId,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '0！' })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getgradeId: function (req, res) {
        var userId = req.user.userId
        userDAO.getgradeId(userId,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '0！' })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getAllevaluateuser: function (req, res) {
        var orderID = req.body.orderID
        userDAO.getAllevaluateuser(orderID,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '0！' })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getAllstory: function (req, res) {
        var userId = req.user.userId
        userDAO.getAllstory(userId,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '0！' })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getAllintegral: function (req, res) {
        var userId = req.body.userId
        userDAO.getAllintegral(userId,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '0！' })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getAllintegift: function (req, res) {
        userDAO.getAllintegift(function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '0！' })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getAllorders: function (req, res) {
        var userId = req.body.userId
        userDAO.getAllorders(userId,function (err, results) {
            if (err) {
                res.json({code: 500, data: [], msg: '0!' })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    uploadPic: function (req, res) {
        var form = new formidable.IncomingForm()    //创建上传表单对象
        form.uploadDir = path.join(__dirname, '..', '/public/upload')         //设置上传文件的路径
        form.keepExtensions = true                  //设置保留上传文件的扩展名
        form.parse(req, function (err, fields, files) {
            if (err) {
                res.send('文件上传错误！')
            }
            console.log('...................')
            //fields是上传的表单字段数组，files是上传的文件列表
            console.log("aaa:"+path.parse(files.head.path).base)
            //保存图片路径到数据库
            //1.获取当前用户编号
            var userId1 = req.user.userId
            // console.log(userId1)
            // var user = {userId:fields.userId,headPic:path.parse(files.flie.path).base}
            // var userId = req.body.userId
            //2.获取当前用户的图片名称
            var headPic = path.parse(files.head.path).base
            // console.log(headPic)
            var user = {headPic,userId1}
            userDAO.upload(user, function (err, results) {
                console.log(12)
                   console.log(results)
                if (err) {
                    res.json({ code: 500,data:[], msg: '0！' })
                } else {
                    res.json({ code: 200, data: results, msg: '1！' })
                }
            })
        })

    },
    changeuser: function (req, res) {
        var userId = req.user.userId
        var userName = req.body.userName
        var address = req.body.address
        var birthday = new Date (req.body.birthday).toLocaleDateString()
        var sex = req.body.sex
        var email = req.body.email
        console.log(111)
        console.log(req.body)
        userDAO.changeuser({
            userId: userId, userName: userName, address: address,
            sex: sex, email: email, birthday: birthday,
         
        }, 
       
        function (err, results) {
            console.log(results)
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '用户修改失败!' })
            } else {
                if (results.affectedRows == 0) {
                    res.json({ code: 500,data:[], msg: '0' })
                } else {
                    res.json({ code: 200, data: results, msg: '1' })
                }
            }
        })
    },
    getusertel: function (req, res) {
        var telephone = req.query.telephone
        var userId = req.query.userId
        userDAO.getusertel({ telephone: telephone, userId: userId }, function (err, results) {
            if (telephone.length == 11) {
                if (err) {
                    console.log(err)
                    res.json({ code: 500, msg: '手机号修改失败!' })
                } else {
                    if (results.affectedRows == 0) {
                        console.log(results)
                        res.json({ code: 500, data:[],msg: '0' })
                    } else {
                        res.json({ code: 200, data: results, msg: '1' })
                    }
                }
            } else {
                res.json({ code: 500, msg: '手机号不正确' })
            }
        })
    },
    getuserpass: function (req, res) {
        var users = { userpwd: req.body.userPassword1,newuserpwd: req.body.userPassword2,userId:req.user.userId}
        userDAO.seluserpass(users,function(err,results){
            // console.log(results)
            bcrypt.compare(users.userpwd, results[0].userPassword, function (err, resPwd) {
                console.log(users.userpwd)
                console.log(results[0].userPassword)
                console.log(resPwd)
                if (resPwd) {
                        // res.status(200).json({ msg: '原密码正确'})
                        if(results != null){
                            // console.log(results)
                            bcrypt.genSalt(10, function (err, salt) {
                                bcrypt.hash(users.newuserpwd, salt, function (err, hash) {
                                    users.newuserpwd = hash
                                    // console.log(users)
                                    userDAO.getuserpass(users, function (err, results) {
                                        if (err) {
                                            // console.log(err)
                                            res.json({ code: 500, msg: '密码修改失败!' })
                                        } else {
                                            if (results.affectedRows == 0) {
                                                // console.log(results)
                                                res.json({ code: 500,data:[], msg: '0' })
                                            } else {
                                                res.json({ code: 200, data: results, msg: '1' })
                                            }
                                        }
                                    });
                                });
                            })
                        }
                } else {
                    res.status(500).json({code: 500, msg: '原密码错误，请重新输入！！' })
                }
            })      
        })
    },
    getAllintecount: function (req, res) {
        var userId = req.body.userId
        userDAO.getAllintecount(userId,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: err.message })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getvipcount: function (req, res) {
        var userId = req.body.userId
        userDAO.getvipcount(userId,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: err.message })
            } else {
                res.json({ code: 200, data: results, msg: '1!' })
            }
        })
    },
    getinvestmoney: function (req, res) {
        var invest = { investmoney :req.body.investmoney,invsetTime :req.body.invsetTime,userID :req.body.userID}
        userDAO.getinvestmoney(invest,function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '充值失败！' })
            } else {
                res.json({ code: 200, data: results, msg: '充值成功!' })
            }
        })
    },
}
module.exports = userController